<?php
class Vfriend_Captcha_Image
{
	static $_session_name = SESSION_CAPTCHA;
	function getCaptchaId($session_name = null)
	{
		if ($session_name) {
			self::$_session_name = $session_name;
		}
		$strOption = 'captcha';		
		$ROOT_DIR 	= realpath('../' . dirname(basename(__FILE__)));
		$objConf 	= new Zend_Config_Ini(ROOT_DIR . '/configs/config.ini', $strOption);
		$arrConf 	= $objConf->captcha->toArray();
		$objCaptcha = new Zend_Captcha_Image();
		$objCaptcha->setFont($ROOT_DIR . $arrConf['font']);
		$objCaptcha->setImgDir($ROOT_DIR . $arrConf['imgdir']);
		$objCaptcha->setImgUrl($arrConf['imgurl']);
		$objCaptcha->setSuffix($arrConf['suffix']);	
		$objCaptcha->setTimeout('800');	
		$objCaptcha->setWordLen('6');
		$objCaptcha->setHeight('40');
		$objCaptcha->setWidth('140');
		$objCaptcha->setFontSize('20');
		$objCaptcha->setDotNoiseLevel(500);
		$objCaptcha->setLineNoiseLevel(0);
		$objCaptcha->generate8();		
		$strId = $objCaptcha->getId();
		$_SESSION[self::$_session_name] = md5(Vfriend_Captcha_Image::getCaptchaWord($strId));
		return $strId;
	}

	public function getCaptchaWord($strCaptchaId)
	{
		$objCaptchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $strCaptchaId);		
		$objCaptchaIterator = $objCaptchaSession->getIterator();		
		if (isset($objCaptchaIterator['word'])) {
			$strCaptchaWord = $objCaptchaIterator['word'];
			return $strCaptchaWord;
		}
		return '';
	}

	public function validate($strCaptchaInput = '', $session_name = null)
    {
		if ($session_name != null) {
			self::$_session_name = $session_name;
		}	
		if ($_SESSION[self::$_session_name] == md5($strCaptchaInput)) {
			return 1;
		}
		return 0;
    }
}
?>